public class BST {
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;


    //判断val是否存在于二叉搜索树中
    public boolean search(int val){
        //一直比较，直到找到或者找不到
        TreeNode cur=root;
        while(cur!=null){
            if(cur.val<val){
                cur=cur.right;
            }
            else if(cur.val>val){
                cur=cur.left;
            }
            else{
                return true;
            }
        }
        return false;
    }

    //搜索二叉树的插入
    public void insert(int val){
        TreeNode node=new TreeNode(val);
        if(root==null){
            root=node;
            return ;
        }
        TreeNode parent=null;
        TreeNode cur=root;
        while(cur!=null){
            if(cur.val<val){
                parent=cur;
                cur=cur.right;
            }
            else if(cur.val>val){
                parent=cur;
                cur=cur.left;
            }
            else{
                return ;   //一样的值无法重复插入，直接返回
            }
        }
        if(parent.val<val){
            parent.right=node;
        }
        else{
            parent.left=node;
        }
    }

    //搜索二叉树的删除
    public void remove(int val){
        TreeNode cur=root;
        TreeNode parent=null;
        if(root==null){
            return ;
        }
        while(cur!=null){
            if(cur.val<val){
                parent=cur;
                cur=cur.right;
            }
            else if(cur.val>val){
                parent=cur;
                cur=cur.left;
            }
            else{
                removeNode(parent,cur);
                return ;
            }
        }
    }
    public void removeNode(TreeNode parent,TreeNode cur){
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            }
            else if(cur==parent.left){
                parent.left=cur.right;
            }
            else{
                parent.right=cur.right;
            }
        }
        else if(cur.right==null){
            if(cur==root){
                root=cur.left;
            }
            else if(cur==parent.left){
                parent.left=cur.left;
            }
            else{
                parent.right=cur.left;
            }
        }
        else{
            TreeNode target=cur.right;
            TreeNode targetParent=cur;
            while(target!=null){
                targetParent=target;
                target=target.left;
            }
            cur.val=target.val;
            if(target==targetParent.right){
                targetParent.right=target.right;
            }
            else{
                targetParent.left=target.right;
            }

        }
    }

    public static void main(String[] args) {
        BST binarySearchTree=new BST();
        int[] arr=new int[]{5,10,3,1,15};
        for(int x:arr){
            binarySearchTree.insert(x);
        }
    }
}
